<?php
// +----------------------------------------------------------------------
// | makePHP
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    Mirze<mirze@foxmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_sell.class.php 2011-07-07 11:16:26 $
 * @Show Note: 批复执行
 */
Class zy_sell extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;

	/**
	 *	Construct this module class.
	*/
	public function zy_sell()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_sell.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_sell");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		$this->smarty->assign('sell_type_arr',get_sell_type_arr());//购置性质数组
		$this->smarty->assign('company_arr',get_company_arr());//供货厂商
		$this->smarty->assign("main_menus",$this->get_user_menus());
	}

	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
		$this->zy_sell();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$is_jump = isset($_GET['is_jump']) ? intval($_GET['is_jump']) : 0;
		$is_show_sell = isset($_GET['is_show_sell']) ? intval($_GET['is_show_sell']) : 0;

		$col = "add_time";
		$where = 1;
		//合同编号检索
		$contract_no = isset($_GET['contract_no']) ? trim($_GET['contract_no']) : '';
		if (!empty($contract_no)) {
			$where .= " AND contract_no like '%{$contract_no}%' ";
		}
		//谈判人检索
		$admin_user_id = isset($_GET['admin_user_id']) ? trim($_GET['admin_user_id']) : '';
		if (!empty($admin_user_id)) {
			$where .= " AND admin_user_id = {$admin_user_id} ";
		}
		//供应商检索
		$sell_owner = isset($_GET['sell_owner']) ? trim($_GET['sell_owner']) : '';
		if (!empty($sell_owner)) {
			$where .= " AND sell_owner LIKE '%{$sell_owner}%' ";
		}
		//签订合同日期
		$contract_time_start = isset($_GET['contract_time_start']) && !empty($_GET['contract_time_start']) ? strtotime(($_GET['contract_time_start'])) : '';
		//批复日期
		$contract_time_end = isset($_GET['contract_time_end']) && !empty($_GET['contract_time_end']) ? strtotime($_GET['contract_time_end']) : '';

		if (!empty($contract_time_start)) {
			$where .= " AND contract_time >= {$contract_time_start} ";
		}

		if (!empty($contract_time_end)) {
			$contract_time_end = $contract_time_end + 86400 - 1 ;//多加一天
			$where .= " AND contract_time <= {$contract_time_end} ";
		}
		
		//如果是添加验收
		if($is_show_sell){
			$where .= " and sell_state=0";
		}
		//echo $where;
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);
		//取得执行记录表中虚拟设备信息
		/*$v_mc_info = array();
		$v_mc_info_log = array();
		foreach ($arr as $key=>$value)
		{
		$v_mc_info[$value['contract_no']] = $this->get_v_mc_info($value['contract_no']);//根据合同编号取得设备信息
		$v_mc_info_log[$value['contract_no']] = $this->get_mc_log_arr($value['contract_no']);//根据合同编号取得执行设备信息
		}*/
		//print_r($v_mc_info_log);
		$this->smarty->assign('arr',$arr);
		/*$this->smarty->assign('v_mc_info',$v_mc_info);//二级设备信息
		$this->smarty->assign('v_mc_info_log',$v_mc_info_log);//三级设备信息*/
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		//$this->smarty->assign('act','list');
		if (empty($is_jump)) {
			$this->smarty->assign('act','list');
		}else{
			$this->smarty->assign('is_show_sell',$is_show_sell);
			$this->smarty->assign('act','sel');
		}
		$this->smarty->display($this->tplFile);
	}

	/**
	 *	add method.
	*/
	public function add()
	{
		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			/*$investment_no = isset($_REQUEST['investment_no']) ? htmlspecialchars(trim($_REQUEST['investment_no'])) : '';//投资自编号
			$v_mc_arr = array();
			if (!empty($investment_no)) {
			//展示已经批复的虚拟设备信息
			$v_mc_arr = $this->get_reply_mc($investment_no);
			if (empty($v_mc_arr)) {
			alertMsg('-5014');
			}
			$key_arr = array_keys($v_mc_arr);
			$coun = array_pop($key_arr);
			$this->smarty->assign('coun',$coun);
			//print_r($v_mc_arr);
			}*/
			$plan_reply_id_arr = isset($_GET['plan_reply']) ? $_GET['plan_reply'] : array();
			$v_mc_arr = array();
			$plan_reply_no_arr = array();
			$contract_cost = 0;
			//如果存在批复ID数组 则取未批复的设备信息
			if ($plan_reply_id_arr) {
				$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
				$v_mc_arr = $zy_plan_reply_mc_db->get_mc_info_by_plan_id_arr($plan_reply_id_arr);
				//print_r($v_mc_arr);
				//循环接收选择的批复ID 的 批复单号
				foreach ($plan_reply_id_arr as $value)
				{
					$plan_reply_no_arr[$value]  = isset($_GET['plan_reply_no_'.$value]) ? $_GET['plan_reply_no_'.$value] : array();
				}
				//取得合同总价
				foreach ($v_mc_arr as $v)
				{
					$v_mc_id_arr[] = $v['v_mc_id'];
				}
				if (!empty($v_mc_id_arr)) {
					$zy_mc_type_db = Factory::getDB('zy_mc_type');
					$v_arr = $zy_mc_type_db->get_v_mc_info($v_mc_id_arr);//取得设备类型信息数组
					foreach ($v_arr as $val)
					{
						$contract_cost += $val['make_cost'] + $val['manage_cost'] + $val['vat'] ;
					}
				}
				$key_arr = array_keys($v_mc_arr);
				$coun = array_pop($key_arr);
				//print_r($v_mc_arr);
			}
			//print_r($v_mc_arr);
			//使用模版，展示提交界面
			//$this->smarty->assign('investment_no',$investment_no);//投资自编号
			$this->smarty->assign('coun',$coun);
			$this->smarty->assign('v_mc_arr',$v_mc_arr);//在该投资计划内未完成设备数组
			$this->smarty->assign('plan_reply_no_arr',$plan_reply_no_arr);//选择的批复名称数组
			$this->smarty->assign('contract_cost',$contract_cost);//合同总价
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			$temp = $this->getValue();
			//取出执行的设备关联表中ID数组
			$reply_mc_id_arr = $temp['reply_mc_id_arr'];
			unset($temp['reply_mc_id_arr']);
			//取得无子设备数量
			$reply_do_num = array();
			if (isset($temp['reply_do_num'])) {
				$reply_do_num = $temp['reply_do_num'];
				unset($temp['reply_do_num']);
			}
			$insrt = $this->db->insert_zy_sell($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			//批复与执行关联表处理
			$this->sell_reply_process($reply_mc_id_arr,$reply_do_num,$temp['contract_no']);
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($insrt < 0) {
				alertMsg($error_no, 'index.php?c=zy_sell');
			}else{
				redirectPage('index.php?c=zy_sell');
			}
			//alertMsg($error_no, 'index.php?c=zy_sell');
		}
	}

	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$contract_no = isset($_REQUEST['contract_no']) ? trim($_REQUEST['contract_no']) : '' ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$result = $this->db->getOne($contract_no);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_sell');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			//zaki 2011-9-10  批复设备 和批复单号处理
			//批复设备关联ID数组
			$zy_sell_reply_db = Factory::getDB('zy_sell_reply');
			$temp_arr = $zy_sell_reply_db->get_plan_reply_info($contract_no);//取得批复和执行数据
			if (empty($temp_arr)) {
				alertMsg('-1000');
			}
			//取得批复id数组和批复单号数组 主要是符合原来程序
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			foreach ($temp_arr as $key=>$value) {
				$plan_arr = $zy_plan_reply_mc_db->get_plan_info($value['plan_reply_mc_id']);//取得批复信息
				$plan_reply_id_arr[] = $plan_arr['plan_reply_id'] ;
				$plan_reply_no_arr[$plan_arr['plan_reply_id']] = $plan_arr['plan_reply_no'];
			}
			//print_r($plan_reply_id_arr);
			$v_mc_arr = $zy_plan_reply_mc_db->get_mc_info_by_plan_id_arr($plan_reply_id_arr,1);
			//取得合同总价
			foreach ($v_mc_arr as $v)
			{
				$v_mc_id_arr[] = $v['v_mc_id'];
			}
			if (!empty($v_mc_id_arr)) {
				$zy_mc_type_db = Factory::getDB('zy_mc_type');
				$v_arr = $zy_mc_type_db->get_v_mc_info($v_mc_id_arr);//取得设备类型信息数组
				foreach ($v_arr as $val)
				{
					$contract_cost += $val['make_cost'] + $val['manage_cost'] + $val['vat'] ;
				}
			}
			$v_mc_arr = $this->get_v_mc_info($contract_no);
			$key_arr = array_keys($v_mc_arr);
			$coun = array_pop($key_arr);
			$this->smarty->assign('coun',$coun);
			//print_r($v_mc_arr);
			$this->smarty->assign('v_mc_arr',$v_mc_arr);//在该投资计划内未完成设备数组
			$this->smarty->assign('plan_reply_no_arr',$plan_reply_no_arr);//选择的批复名称数组
			$this->smarty->assign('contract_cost',$contract_cost);//合同总价


			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "contract_no = '{$contract_no}' ";
			$temp = $this->getValue(1);
			//print_r($temp);exit;
			$update = $this->db->update_zy_sell($temp,$where);
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($update < 0) {
				alertMsg($error_no, 'index.php?c=zy_sell');
			}else{
				redirectPage('index.php?c=zy_sell');
			}
			//alertMsg($error_no, 'index.php?c=zy_sell');
		}
	}
	
	public function auth_finsh()
	{
		$contract_no = isset($_GET['contract_no']) ? trim($_GET['contract_no']) : '';
		$where = "contract_no='{$contract_no}'";
		$arr = $this->db->getOne($contract_no);
		//print_r($arr);
		if($arr['sell_state']==0){
			$array['sell_state'] = 1;
		}else{
			$array['sell_state'] = 0;
		}
		$this->db->update_zy_sell($array,$where);
		redirectPage();
		
	}

	/**
	 *	浏览二级设备（设备类型）
	*/
	public function browse() {
		$contract_no = isset($_REQUEST['contract_no']) ? trim($_REQUEST['contract_no']) : '' ;
		//使用模版，展示提交界面
		$result = $this->db->getOne($contract_no);
		if(empty($result))
		{
			alertMsg('-2001', 'index.php?c=zy_sell');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}
		$v_mc_info = $this->get_v_mc_info($contract_no);//根据合同编号取得设备信息
		//分页，通过查询所有来去
		$rowNo = count($v_mc_info);
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		//unset($zy_plan_reply_mc_arr);
		for ($i=$offset;$i<($numrows+$offset);$i++){
			if(isset($v_mc_info[$i]))
			$page_arr[] = $v_mc_info[$i];
		}
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->assign('v_mc_info',$page_arr);//二级设备信息
		//print_r($v_mc_info);
		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->assign('contract_no',$contract_no);
		$this->smarty->display($this->tplFile);
	}
	/**
	 * 浏览三级设备（具体设备）
	 *
	 */
	public function browse_equipment() {
		$contract_no = isset($_REQUEST['contract_no']) ? trim($_REQUEST['contract_no']) : '' ;
		$sell_reply_id = isset($_REQUEST['sell_reply_id']) ? intval($_REQUEST['sell_reply_id']) : 0 ;
		$v_mc_info_log = $this->get_mc_log_arr($contract_no);//根据合同编号取得执行设备信息
		//print_r($v_mc_info_log);
		//print_r($v_mc_info_log[$sell_reply_id]);
		//暂时到货数
		$v_mc_info = $v_mc_info_log[$sell_reply_id];
		if ($v_mc_info_log[$sell_reply_id]) {
			$zy_arrival_log_db = Factory::getDB('zy_arrival_log');
			foreach ($v_mc_info as $key=>$value)
			{
				$v_mc_info[$key]['mc_log_num'] = $zy_arrival_log_db->get_arrival_no($value['mc_log_id']);
			}
		}
		//取得到货记录
		$zy_arrival_log_db = Factory::getDB('zy_arrival_log');
		$arrival_arr = $zy_arrival_log_db->get_arrival_info_by_sell_reply_id($sell_reply_id);

		$this->smarty->assign('v_mc_info_log',$v_mc_info);//三级设备信息
		//print_r($v_mc_info);
		$this->smarty->assign('arrival_arr',$arrival_arr);//到货记录信息

		//print_r($arrival_arr);
		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act','browse_equipment');
		$this->smarty->assign('contract_no',$contract_no);
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$contract_no = isset($_REQUEST['contract_no']) ? intval(trim($_REQUEST['contract_no'])) : 0 ;
		$where = "contract_no=$contract_no";
		$del=$this->db->delete_zy_sell($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if ($del < 0) {
			alertMsg($error_no, 'index.php?c=zy_sell');
		}else{
			redirectPage('index.php?c=zy_sell');
		}
		//alertMsg($error_no, 'index.php?c=zy_sell');
	}

	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		if($isEdit){

		} else {
			$temp['contract_no'] = isset($_POST['contract_no']) ? htmlspecialchars(trim($_POST['contract_no'])) : '';//合同编号
			if (empty($temp['contract_no'])) {
				alertMsg('-2069');
			}
			//查看合同编号是否重复
			if ($this->db->getOne($temp['contract_no'])) {
				alertMsg('-2070');
			}
			$temp['investment_no'] = isset($_POST['investment_no']) ? htmlspecialchars(trim($_POST['investment_no'])) : '';//投资自编号
			/*if (empty($_POST['investment_no'])) {
			alertMsg('-2055');
			}*/
			$temp['add_time'] = time();
			$temp['reply_mc_id_arr'] = isset($_POST['reply_mc_id_arr']) ? $_POST['reply_mc_id_arr'] : array();//接收执行设备数组
			if (empty($temp['reply_mc_id_arr'])) {
				alertMsg('-2072');
			}
			//展示已经批复的虚拟设备信息
			$v_mc_arr = $this->get_reply_mc_by_id($temp['reply_mc_id_arr']);
			//循环接受对应虚拟设备ID的批复数量数组
			foreach ($temp['reply_mc_id_arr'] as $value) {
				$reply_do_num = isset($_POST["reply_do_num_{$value}"]) && !empty($_POST["reply_do_num_{$value}"]) ? intval($_POST["reply_do_num_{$value}"]) : 0;//取得执行机器数量
				//判断 如果为无子设备 1 数量不能为空 2 不能超过批复数
				if (!isset($v_mc_arr[$value])) {
					alertMsg('-2073');
				}
				if ($v_mc_arr[$value]['is_p_mc'] == 0 && empty($reply_do_num)) {
					alertMsg('-2074');
				}
				if ($v_mc_arr[$value]['is_p_mc'] == 0 && $v_mc_arr[$value]['plan_reply_mc_number'] < $reply_do_num) {
					alertMsg('-2075');
				}
				if ($v_mc_arr[$value]['is_p_mc'] == 0 && !empty($reply_do_num) && $v_mc_arr[$value]['plan_reply_mc_number'] >= $reply_do_num) {
					$temp['reply_do_num'][$value] = $reply_do_num;
				}
			}
		}

		//补充其他的字段
		$temp['contract_prices'] = isset($_POST['contract_prices']) ? sprintf("%01.2f",$_POST['contract_prices']) : 0 ;//合同总价
		/*$temp['price_type'] = $_POST['price_type'];//币种*/
		$temp['sell_owner'] = isset($_POST['sell_owner']) ? htmlspecialchars(trim($_POST['sell_owner'])) : '';//供应商
		$temp['talks_time'] = isset($_POST['talks_time']) && !empty($_POST['talks_time']) ? strtotime($_POST['talks_time']) : 0;//谈判日期
		$temp['contract_time'] = isset($_POST['contract_time']) && !empty($_POST['contract_time']) ? strtotime($_POST['contract_time']) : 0;//签订合同日期
		$temp['finish_time'] = isset($_POST['finish_time']) && !empty($_POST['finish_time']) ? strtotime($_POST['finish_time']) : 0;//到货日期
		$temp['contract_info'] = isset($_POST['contract_info']) ? htmlspecialchars(trim($_POST['contract_info'])) : '';//备注
		$temp['currency_id'] = isset($_POST['currency_id']) ? intval($_POST['currency_id']) : 1;//币种
		$temp['admin_user_id'] = isset($_POST['admin_user_id']) ? intval($_POST['admin_user_id']) : 0;
		if (empty($temp['admin_user_id']))
		{
			alertMsg('-2046');
		}
		$temp['exchange_price'] = isset($_POST['exchange_price']) ? sprintf("%01.2f",$_POST['exchange_price']) : 0 ;//外汇总价
		$temp['exchange_rate'] = isset($_POST['exchange_rate']) ? sprintf("%01.2f",$_POST['exchange_rate']) : 0 ;//汇率
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
		/* 定义该类中文名称*/
		'class_name'=>array(__CLASS__,'计划执行'),
		'class_method' =>array(
		//array（方法名，中文解释，权值,是否为菜单）
		'show'  => array('show','列表',1,true),
		'add'  => array('add','添加',2,true),
		'edit'  => array('edit','编辑',4,false),
		'del'  => array('del','删除',8,false),
		'browse' => array('browse', '查看', 16, false),
		'browse_equipment' => array('browse_equipment', '详细', 32, false),
		'updateFile' => array('updateFile', '详细', 64, false),
		'auth_finsh' => array('auth_finsh', '验收完成', 128, false),
		
		'show_p'=> array('show_p','',0,false)
		)
		);
	}
	function updateFile()
	{
		$file_path = isset($_FILES["file_path"]) ? $_FILES["file_path"]: '';//附件名称
		$contract_no = isset($_REQUEST['contract_no']) ? trim($_REQUEST['contract_no']) : 0 ;
		$error_no = 0;
		//判断是否存在文件上传
		if (!empty($file_path['name'])) {
			$array = array(
			'source_file_name'=>$file_path['name'],
			);
			$file_name = upload_file($_FILES,'file_path',md5($file_path['name'].time()),$_GET['c']);
			$array['file_path'] = $file_name;
			$where = " contract_no = '{$contract_no}' ";
			$update = $this->db->update_zy_sell($array,$where);
			$error_no = 0 > $update ?  $update :'-1502';
		}
		if ($error_no == '-1502') {
			echo json_encode(array('code'=>1));exit;
		}else{
			echo json_encode(array('code'=>2));exit;
		}

	}
	/**
	 * 根据投资计划编号取得未完成的设备信息
	 *
	 * @param unknown_type $investment_no
	 * @return unknown
	 */
	function get_reply_mc($investment_no)
	{
		$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
		return $zy_plan_reply_mc_db->get_mc_by_investment_no($investment_no);
	}
	/**
	 * 根据批复关联表中ID数组 取得关联表中信息
	 *
	 * @param unknown_type $plan_reply_mc_id_arr
	 * @return unknown
	 */
	function get_reply_mc_by_id($plan_reply_mc_id_arr)
	{
		$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
		return $zy_plan_reply_mc_db->get_mc_by_id($plan_reply_mc_id_arr);
	}
	/**
	 * 批复执行关联表  执行记录表 数量记录表 处理
	 *
	 * @param unknown_type $reply_mc_id_arr  批复设备关联表ID数组
	 * @param unknown_type $reply_do_num  无子设备对应执行数量数组
	 * @param unknown_type $contract_no 合同编号
	 */
	function sell_reply_process($reply_mc_id_arr,$reply_do_num=array(),$contract_no)
	{
		//声明批复与执行关联表数组
		$values = array(
		'contract_no'=>$contract_no,
		);
		$zy_sell_reply_db = Factory::getDB('zy_sell_reply');
		$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
		$zy_mc_type_db = Factory::getDB('zy_mc_type');
		$zy_mc_log_db = Factory::getDB('zy_mc_log');
		$zy_no_log_db = Factory::getDB('zy_no_log');
		$zy_plan_reply_db = Factory::getDB('zy_plan_reply');
		foreach ($reply_mc_id_arr as $k=>$v)
		{
			$temp = $zy_plan_reply_mc_db->getOne($v);
			$values['project_id'] = $temp['project_id'];
			$values['v_mc_id'] = $temp['v_mc_id'];
			$values['plan_reply_mc_id'] = $v;
			$re = $zy_sell_reply_db->insert_zy_sell_reply($values);

			//更新批复设备关联表中执行状态
			$where_plan_mc = " plan_reply_mc_id = {$v} ";
			$zy_plan_reply_mc_db->update_zy_plan_reply_mc(array('sell_state'=>1),$where_plan_mc);

			//判断被批复的设备是否都被执行 如果都被执行 则改变批复表中状态
			$where_plan_reply_mc = " plan_reply_id = {$temp['plan_reply_id']} AND sell_state = 0 ";
			$plan_reply_mc_arr = $zy_plan_reply_mc_db->getAll($where_plan_reply_mc,'plan_reply_mc_id',1,0);
			if (empty($plan_reply_mc_arr)) {
				//更改批复表中执行状态
				$where_plan = " plan_reply_id = {$temp['plan_reply_id']} ";
				$zy_plan_reply_db->update_zy_plan_reply(array('reply_state'=>1),$where_plan);
			}

			if ($re > 0 && $temp['is_p_mc'] == 0 && isset($reply_do_num[$v])) {
				//取得虚拟设备信息
				$v_mc_arr = $zy_mc_type_db->getOne($temp['v_mc_id']);
				$mc_log_arr = array(
				'sell_reply_id'=>$re,
				'mc_cn_name'=>$v_mc_arr['mc_cn_name'],
				'mc_en_name'=>$v_mc_arr['mc_en_name'],
				'mc_model'=>$v_mc_arr['mc_type_model'],
				'mc_no'=>$reply_do_num[$v],
				'arrival_state'=>0,
				'auth_state'=>0,
				'price'=> sprintf("%01.2f",($v_mc_arr['make_cost']+$v_mc_arr['manage_cost']+$v_mc_arr['vat'])/$v_mc_arr['plan_no']),
				'unit'=>$v_mc_arr['unit']
				);
				//插入执行设备记录表
				$mc_log_id = $zy_mc_log_db->insert_zy_mc_log($mc_log_arr);
				if ($mc_log_id > 0) {
					$no_log_arr = array(
					'mc_log_id'=>$mc_log_id,
					'sell_no'=>$reply_do_num[$v],
					'arrival_no'=>0,
					'auth_no'=>0,
					'transfer_no'=>0,
					'allocation_no'=>0,
					);
					$zy_no_log_db->insert_zy_no_log($no_log_arr);
				}
			}
		}
		return true;
	}
	/**
	 * 根据合同号取得虚拟设备信息
	 *
	 * @param unknown_type $contract_no
	 * @return unknown
	 */
	function get_v_mc_info($contract_no)
	{
		$return_arr = array();
		$zy_sell_reply_db = Factory::getDB('zy_sell_reply');
		$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
		$zy_plan_reply_db = Factory::getDB('zy_plan_reply');
		//取得批复设备关联表数组
		$plan_reply_info_arr = $zy_sell_reply_db->get_plan_reply_info($contract_no);
		foreach($plan_reply_info_arr as $value)
		{
			//根据批复设备链表	ID取得设备信息
			$temp = $zy_plan_reply_mc_db->get_mc_info_by_id_arr($value['plan_reply_mc_id']);
			if($temp){
				$temp[0]['sell_reply_id'] = $value['sell_reply_id'];//批复执行关联ID
				$plan_reply_arr = $zy_plan_reply_db->getOne($temp[0]['plan_reply_id']);//取得批复单号
				$temp[0]['plan_reply_no'] = $plan_reply_arr['plan_reply_no'];
				$return_arr[] = $temp[0];
			}
		}
		return $return_arr;
	}
	/**
	 * 根据合同编号取得设备记录信息
	 *
	 * @param unknown_type $contract_no
	 */
	function get_mc_log_arr($contract_no)
	{
		$return_arr = array();
		$zy_sell_reply_db = Factory::getDB('zy_sell_reply');
		$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
		$zy_mc_log_db = Factory::getDB('zy_mc_log');
		$zy_arrival_log_db = Factory::getDB('zy_arrival_log');
		//根据合同号取得批复执行管理表数组
		$plan_reply_info_arr = $zy_sell_reply_db->get_plan_reply_info($contract_no);
		foreach($plan_reply_info_arr as $value)
		{
			//根据批复执行链表	ID取得执行记录信息
			$temp = $zy_mc_log_db->get_mc_log_info($value['sell_reply_id']);
			//展示数据整理
			if (!empty($temp)) {
				foreach ($temp as $k=>$v)
				{
					//取得到货地点
					$arrival_arr = $zy_arrival_log_db->get_arrival_info($v['mc_log_id']);
					if (!empty($arrival_arr)) {

						$temp[$k]['arrival_area'] = $arrival_arr['arrival_area'];
					}
				}
			}
			$return_arr[$value['sell_reply_id']] = $temp;
		}

		return $return_arr;
	}
}

?>